<?php
namespace Dzdjwyadmin\Controller;
// use Think\Controller;
class IndexController extends \Think\Controller {
    
    public function index()
    {
    	if(!session('admin_id')){
            $this->redirect('/dzdjwyadmin/index/login');die;
    	}

        $id = session('admin_id');
        $info = M('admin');
        $name= $info->where('id = '.$id)->getField('username');

        $this->assign('name',$name);

    	$this->display();
    }


/*-----------order----------------*/
    //订单列表
    public function order()
    {
        if(!session('admin_id')){
            $this->redirect('/dzdjwyadmin/index/login');die;
        }

    	$information = M('main');
        $count = $information->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,11);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        
        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $res = $information->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
        
        foreach ($res as $k => $v) {
            $res[$k]['photo'] = explode('*',$v['photo']);
        }
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('data',$res);// 赋值数据集

        
        $this->display();
    }
    //查看订单
    public function seeOrder()
    {
        if(!session('admin_id')){
            $this->redirect('/dzdjwyadmin/index/login');die;
        }

        $data = $this->datas($_GET['orderId']);

        $this->assign('data',$data);

        $this->display();
    }
    public function datas($oid)
    {
        $demo = !empty($oid) ? 'where id='.$oid : ' ';

        // var_dump($demo);die;

        $information = M('main');

        $data = $information->query('select * from djwy_main '.$demo);
        
        // var_dump($data);die;
        foreach ($data as $k => $v) {
            $data[$k]['photo'] = explode('*',$v['photo']);
        }

        return $data;
    }

/*------------Search-------------------*/
    //在查找时 走此方法
    public function search()
    {
        $info = M('main');

        $mess = $_GET['content'];
        // var_dump($mess);die;
        $count = $info->where('username like "%'.$mess.'%"')->count();// 查询满足要求的总记录数
        // var_dump($count);die;
        $Page = new \Think\Page($count,6);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        
        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $a = $info->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->where('username like "%'.$mess.'%"')->select();
        
        foreach ($a as $k => $v) {
            $a[$k]['photo'] = explode('*',$v['photo']);
        }
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('data',$a);// 赋值数据集

        $this->display('order');
    }


    //处理
    public function chuli()
    {
        $a = M('main');

        $data = $a->where('id = '.$_POST['id'])->save(['status'=>1]);

        if($data){    
            $status['status'] = true;
        }else{
            $status['status'] = false;
        }

        echo json_encode($status);
    }
    //完成
    public function wancheng()
    {
        $a = M('main');

        $data = $a->where('id = '.$_POST['id'])->save(['status'=>2]);

        if($data){    
            $status['status'] = true;
        }else{
            $status['status'] = false;
        }

        echo json_encode($status);
    }


/*--------------login------------------*/
    public function login()
    {
       
        $this->display();
    }

    //验证
    public function checkLogin()
    {

        $id = session('admin_id');
        $name = I('post.username');
        $pass = I('post.password');

        $info = M('admin');

        // $data = $info->where('id='.$id.' AND status= 1 AND username="'.$name.'"')->getField('id,username,password');
        $data = $info->query('select username,password from djwy_admin where id='.$id.' AND username="'.$name.'" AND status=1');

        if(password_verify($pass,$data[0]['password'])){
            header('Location:http:./index.html');
        }else{
            header('Location:./login');
        }

    }

    //验证码
    public function verify()
    {
        $Verify = new \Think\Verify();
        $Verify->entry();
    }
    //验证验证码
    public function check()
    {
        $code = I('post.code');
        $name = I('post.name');

        $check['status'] = false;
        if($code){
            $verify = new \Think\Verify();
            // return $verify->check($code, $id);
            if($verify->check($code)){
                $check['status'] = true;
            }else{
                $check['status'] = false;
            }
        }else if($name){
            $info = M('admin');
            // $data = $info->where('username="'.$name.'"')->getField('id,status',true);
            $data = $info->query('select id,status from djwy_admin where username = "'.$name.'" AND status=1');
            if($data[0]['status'] == 1){
                $check['status'] = true;
                //记录管理员 session id
                session('admin_id',$data[0]['id']);
            }else{
                $check['status'] = false;
            }
        }
        echo json_encode($check);
    }

/*--------------logout------------------*/
    public function logout()
    {
        session('admin_id',null);
        header('Location:./login');
    }

}